package com.itheima.wms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.wms.controller.vo.AreaAndLocationListVo;
import com.itheima.wms.controller.vo.TreeAreaLocationVo;
import com.itheima.wms.controller.vo.WareHouseAreaLocation;
import com.itheima.wms.mapper.OwnerMapper;
import com.itheima.wms.pojo.Owner;
import com.itheima.wms.pojo.OwnerPageDto;
import com.itheima.wms.service.OwnerService;
import com.itheima.wms.utils.PageBean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class OwnerServiceImpl implements OwnerService {

    private final OwnerMapper ownerMapper;

    @Override
    public PageBean ownerPage(OwnerPageDto ownerPageDto) {
        Integer current = ownerPageDto.getCurrent();
        Integer size = ownerPageDto.getSize();
        PageHelper.startPage(current, size);

        Page<Owner> owners = ownerMapper.ownerPage(ownerPageDto);
        int pageSize = owners.getPageSize();
        long total = owners.getTotal();
        PageBean<Owner> ownerPageBean = new PageBean<>(owners, total, pageSize);
        return ownerPageBean;
    }

    @Override
    public void insert(Owner owner) {
        log.info(owner.toString());
        ownerMapper.insert(owner);
    }

    //树形图
    @Override
    public AreaAndLocationListVo findTree(Long warehouseId) {
        //拿到所有数据
        List<WareHouseAreaLocation> list = ownerMapper.findChildList(warehouseId);

        if (CollectionUtil.isEmpty(list)) {
            return AreaAndLocationListVo.builder().build();
        }

        Map<Integer, List<Integer>> areaAndLocationMap = new HashMap<>();
        //收集areaID
        list.forEach(wareHouseAreaLocation -> {
            Integer areaId = wareHouseAreaLocation.getAreaId();
            if (areaAndLocationMap.containsKey(areaId)) {
                areaAndLocationMap.get(areaId).add(wareHouseAreaLocation.getAreaId());
            } else {
                areaAndLocationMap.put(areaId, new ArrayList<>());
            }
        });
//        new TreeAreaLocationVo()


        return null;
    }

    @Override
    public List<Owner> vagueQueryOwner(OwnerPageDto ownerPageDto) {
        List<Owner> ownerList = ownerMapper.vagueQueryOwner(ownerPageDto);

        return ownerList;
    }


}
